import rclpy
from rclpy.node import Node
import sys
from rclpy.logging import get_logger
from rclpy.action import ActionClient
from base_interfaces.action import Progress
class ProgeressActionClient(Node):
    def __init__(self):
        super(). __init__("progress_action_client_node_py")
        self.get_logger().info("客户端节点以创建")
        self.client=ActionClient(self,Progress,"get_sum")
    
    def send_goal(self,num):
        self.client.wait_for_server()
        goal=Progress.Goal()
        goal.num=num
        self.future=self.client.send_goal_async(goal,self.fb_callback)
        self.future.add_done_callback(self.goal_response_callback)
    
    def goal_response_callback(self,future):
        goal_handle=future.result()
        if not goal_handle.accepted:
            self.get_logger().error("目标被拒绝")
            return
        self.get_logger().info("目标被接受正在处理中....")  
        self.result_future=goal_handle.get_result_async()
        self.result_future.add_done_callback(self.get_result_callback)
    
    def get_result_callback(self,future):
        result=future.result().result
        self.get_logger().info("最终相应结果：%d" % result.sum)  
    
    def fb_callback(self,fb_msg):
        progress=fb_msg.feedback.progress
        self.get_logger().info("连续反馈数据:%.2f" % progress)
def main():
    if len(sys.argv)!=2:
        get_logger("rclpy").error("请提交一个整型数据")
        return
    rclpy.init()
    action_client=ProgeressActionClient()
    action_client.send_goal(int(sys.argv[1]))
    rclpy.spin(action_client)
    rclpy.shutdown()    
if __name__=='__main__':
    main()
